<?php
namespace DB;
//数据库类
class DB
{
    protected $host;
    protected $username;
    protected $password;
    protected $dbname;
    protected $conn;
    protected $sql;
    protected $dbprefix;

    public function __construct($host, $username, $password, $dbname, $dbprefix="pre_")
    {
        $this->host = $host;
        $this->username = $username;
        $this->password = $password;
        $this->dbname = $dbname;
        $this->dbprefix = $dbprefix;
        //连接数据库
        $this->connect();
    }

    public function connect()
    {
        $this->conn = mysqli_connect($this->host, $this->username, $this->password);
        if (!$this->conn) {
            die('网站维护中');
        }
        //选择数据库
        mysqli_select_db($this->conn, $this->dbname);
        //设置编码
        mysqli_set_charset($this->conn, 'utf8');
    }

    //查询
    //查询的字段
    public function select($field = "*")
    {
        $this->sql = "SELECT $field";
        return $this;
    }
    //查询的表名
    public function from($tablename)
    {
        $this->sql .= " FROM {$this->dbprefix}$tablename";
        return $this;
    }
    //连表
    public function join($tablename2, $on, $join = "LEFT")
    {
        $this->sql .= " $join JOIN {$this->dbprefix}$tablename2 ON $on";
        return $this;
    }
    //条件
    public function where($conditions)
    {
        $this->sql .= " WHERE $conditions";
        return $this;
    }
    //分组去重
    public function group($group)
    {
        $this->sql .= " GROUP BY $group";
        return $this;
    }
    //排序
    public function order($order)
    {
        $this->sql .= " ORDER BY $order";
        return $this;
    }
    //查询条数
    public function limit($limit)
    {
        $this->sql .= " LIMIT $limit";
        return $this;
    }
    //查询总数
    public function count()
    {
        $res = mysqli_query($this->conn, $this->sql);
        if ($res && mysqli_num_rows($res) > 0) {
            return mysqli_num_rows($res);
        } else {
            return 0;
        }
    }
    //执行sql语句
    //查询多条,返回二维数组
    public function getAll()
    {
        $res = mysqli_query($this->conn, $this->sql);
        $data = array();
        if ($res && mysqli_num_rows($res) > 0) {
            while ($arr = mysqli_fetch_assoc($res)) {
                $data[] = $arr;
            }
        }
        return $data;
    }
    //查询单条,返回一维数组
    public function getOne()
    {
        $res = mysqli_query($this->conn, $this->sql);
        $data = array();
        if ($res && mysqli_num_rows($res) > 0) {
            $data = mysqli_fetch_assoc($res);
        }
        return $data;
    }
    //添加
    public function insert($tablename, $data)
    {
        $this->sql = "INSERT INTO {$this->dbprefix}$tablename ";
        $this->sql .= "(`" . implode('`,`', array_keys($data)) . "`)VALUES";
        $this->sql .= "('" . implode("','", $data) . "')";
        return $this;
    }
    //编辑
    public function save($tablename, $data, $conditions)
    {
        $this->sql = "UPDATE {$this->dbprefix}$tablename SET ";
        foreach ($data as $k => $v) {
            $this->sql .= "`$k`='$v',";
        }
        $this->sql = rtrim($this->sql, ',');
        $this->sql .= " WHERE $conditions";
        return $this;
    }
    //删除
    public function delete($tablename, $conditions)
    {
        $this->sql = "DELETE FROM {$this->dbprefix}$tablename WHERE $conditions";
        return $this;
    }
    //执行
    public function query()
    {
        $res = mysqli_query($this->conn, $this->sql);
        return $res;
    }
    //查看原生SQL语句,目的是为了排除bug
    public function getSql()
    {
        return $this->sql;
    }
}